/**
 * 尊敬的用户，你好：页面 JS 面板是高阶用法，一般不建议普通用户使用，如需使用，请确定你具备研发背景，能够自我排查问题。
 * 我们可以用 JS 面板来开发一些定制度高功能，比如：调用API接口，自定义动作执行等等。
 */

// 当页面渲染完毕后马上调用下面的函数，这个函数是在当前页面 - 设置 - 生命周期 - 页面加载完成时中被关联的。
export function didMount() {
    console.log("「页面 JS」：当前页面地址 " + location.href);
    const self = this;
    const entityCreditAnalysisPieEle = self.$$("zfyy_form_chart_pie_7iz2sy");
    const entityCreditAnalysisLineEle = self.$$("zfyy_form_chart_line_1500df");
    getDataLitigantCreditAnalysis().then(res => {
        initPie(entityCreditAnalysisPieEle, res.result[0]);
    })
    getDataLitigantTrendAnalysis().then(res => {
        initLine(entityCreditAnalysisLineEle, res.result);
    })
}

/**
 * 初始化饼图
 */
function initPie(pieChart, pieData) {

    const transformedData = [
        {
            "name": "91~100分",
            "text": "91~100分",
            "value": parseInt(pieData.count_91_100) || 0,
            "checked": false
        },
        {
            "name": "81~90分",
            "text": "81~90分",
            "value": parseInt(pieData.count_81_90) || 0,
            "checked": false
        },
        {
            "name": "71~80分",
            "text": "71~80分",
            "value": parseInt(pieData.count_71_80) || 0,
            "checked": false
        },
        {
            "name": "61~70分",
            "text": "61~70分",
            "value": parseInt(pieData.count_61_70) || 0,
            "checked": false
        },
        {
            "name": "51~60分",
            "text": "51~60分",
            "value": parseInt(pieData.count_51_60) || 0,
            "checked": false
        },
        {
            "name": "50分及以下",
            "text": "50分及以下",
            "value": parseInt(pieData.count_0_50) || 0,
            "checked": false
        }
    ];

    pieChart.data.props.dataSource.options = transformedData;
}

/**
 * 初始化折线图
 * @param lineChart
 * @param lineData
 */
function initLine(lineChart, lineData) {

    const transformedData = [{
        name: 'red',
        text: '红色主体',
        value: lineData.map(item => ({
            text: item.yearMonth,
            value: item.red_litigant_num || 0
        })),
        checked: false
    }, {
        name: 'yellow',
        text: '黄色主体',
        value: lineData.map(item => ({
            text: item.yearMonth,
            value: item.yellow_litigant_num || 0
        })),
        checked: false
    }, {
        name: 'green',
        text: '绿色主体',
        value: lineData.map(item => ({
            text: item.yearMonth,
            value: item.green_litigant_num || 0
        })),
        checked: false
    }];
    lineChart.data.props.dataSource.options = transformedData;
}

/**
 * 获取饼装图
 */
function getDataLitigantCreditAnalysis() {

    return new Promise((resolve, reject) => {
        const dataModeId = "9a0bdf1d-8455-4799-9554-20582fe1631b"
        api.DATA_SOURCE_EXECUTE(dataModeId, 'ddcat', [], [], '')
            .then(res => {
                resolve(res.result[0]);
            }).catch(() => {
            reject();
        })
    })
}

function getDataLitigantTrendAnalysis() {
    return new Promise((resolve, reject) => {
        const dataModeId = "a64885c4-97a9-4037-a55f-b6905caf0baa";
        api.DATA_SOURCE_EXECUTE(dataModeId, 'ddcat', [], [], '')
            .then(res => {
                resolve(res.result);
            }).catch(() => {
            reject();
        })
    })
}
